package edu.cuit.avatar.generic;

import java.util.Arrays;

/**
 * 受限泛型
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/14 10:38
 */
public class BoundedTypeDemo {
    /**
     * 可以将泛型指定为另外一种类型的子类型。这样的泛型类型称为受限的（bounded）。
     * 比较两个几何图形的面积是否相等
     * @param o1
     * @param o2
     * @return
     * @param <E> 泛型类型，限定为几何图形的类型
     */
    public static<E extends Geometric> boolean equalArea(E o1, E o2){
        return o1.getArea() == o2.getArea();
    }

    public static void main(String[] args) {
        Geometric circle = new Circle(10);
        Geometric rectangle = new Rectangle(5, 8);
        System.out.println(equalArea(circle, rectangle));

        Integer[] intArray = {Integer.valueOf(2),Integer.valueOf(4),Integer.valueOf(6),Integer.valueOf(3),Integer.valueOf(5)};
        Double[] doubleArray = {Double.valueOf(3.14),Double.valueOf(2.14),Double.valueOf(0.14)};
        Character[] charArray = {Character.valueOf('a'),Character.valueOf('c'),Character.valueOf('b'),Character.valueOf('0')};
        String[] strArray = {"Hello","world","peppa"};
        Circle[] circles = {
           new Circle(5), new Circle(10), new Circle(8)
        };
        sort(intArray);
        sort(doubleArray);
        sort(charArray);
        sort(strArray);
        sort(circles);
        System.out.println(Arrays.toString(intArray));
        System.out.println(Arrays.toString(doubleArray));
        System.out.println(Arrays.toString(strArray));
        System.out.println(Arrays.toString(circles));
    }

    public static<T extends Comparable<T>> void sort(T[] list){
        for (int i = 0; i < list.length; i++) {
            T min = list[i];
            int minIndex = i;

            for(int j = i + 1; j < list.length; j++){
                if(min.compareTo(list[j]) > 0){
                    min = list[j];
                    minIndex = j;
                }
            }

            //将最小元素和当前迭代的元素进行交换
            list[minIndex] = list[i];
            list[i] = min;
        }
    }
}
